/*
Copyright (c) 2008, James Punteney All rights reserved.
Code licensed under the BSD License:
http://www.punteney.com/does/license/
*/
if (typeof JP == "undefined" || !JP) { var JP = {};}

/******************
* This code setups up automated image mouseovers
* For more details go to: 
* http://www.punteney.com/does/javascript/image-mouseovers-yui/
*******************/

JP.mouseover = {};

JP.mouseover.config = {
    css_mouseover_class: 'jp-mouseover',
    over_image_text: '_ov',
    element_types: ['img'],
    root_node: '',
    show_broken_img: false
};

JP.mouseover.Mouseover = function (img, config) {
    if ( this instanceof JP.mouseover.Mouseover ) {
        if (img) {
            this.init(img, config); 
        }
    } else {
        return new JP.mouseover.Mouseover(img, config);
    }
};

JP.mouseover.Mouseover.prototype.init = function(img, config) {
    this.mouseover_image = YAHOO.lang.isString(img) ? YAHOO.util.Dom.get(img) : img;
    this.off_src = this.mouseover_image.src;
    this.hover_src = '';
    
    if (!config) {
        config = {};   
    }
    var default_config = {
        over_image_text: '_ov',
        show_broken_img: false
    };
    config = YAHOO.lang.merge(default_config, JP.mouseover.config, config); 
    this.over_text = config.over_image_text;
    this.show_broken_img = config.show_broken_img;

    if (this.off_src.indexOf(".gif") > 0) {
        this.hover_src = this.off_src.replace(".gif", this.over_text+".gif");
    } else if (this.off_src.indexOf(".jpg") > 0) {
        this.hover_src = this.off_src.replace(".jpg", this.over_text+".jpg");
    } else if (this.off_src.indexOf(".png") > 0) {
        this.hover_src = this.off_src.replace(".png", this.over_text+".png");
    }
    this.prep_image();
};

JP.mouseover.Mouseover.prototype.prep_image = function() {
    var self = this;
    this.preloaded_image = new Image();
    YAHOO.util.Event.addListener(this.preloaded_image, 'error', this.image_error, self);
    YAHOO.util.Event.addListener(this.preloaded_image, 'load', this.add_listeners, self); 
    this.preloaded_image.src = this.hover_src;
    
    // Getting a cache of the current image;
    this.off_image = new Image();
    this.off_image.src = this.off_src;
};

JP.mouseover.Mouseover.prototype.add_listeners = function(e, self) {
    if (!self) {
        self = this;
    }
    YAHOO.util.Event.addListener(self.mouseover_image, 'mouseover', self.hover, self); 
    YAHOO.util.Event.addListener(self.mouseover_image, 'mouseout', self.mouseout, self);
};

JP.mouseover.Mouseover.prototype.image_error = function(e, self) {
    // If the image wasn't loaded then set the image_loaded to false
    /// and remove the listeners if they've been applied
    if (self.show_broken_img) {
        self.add_listeners(e);
    }
};

JP.mouseover.Mouseover.prototype.hover = function(e, self) {
    if (self.mouseover_image.src.indexOf(self.over_text) == -1) {
        self.mouseover_image.src = self.hover_src;
    }
};
  
JP.mouseover.Mouseover.prototype.mouseout = function(e, self) {
    if (self.mouseover_image.src.indexOf(self.over_text) > -1) {
        self.mouseover_image.src = self.off_src;
    }
};


JP.mouseover.auto = function(config) {
    if (!config) {
        config = {};   
    }
    var default_config = {
        css_mouseover_class: 'jp-mouseover',
        element_types: ['img'],
        root_node: ''
    };
    // Setting the values from the config object or the JP.mouseover.config, or finally to the defaults
    config = YAHOO.lang.merge(default_config, JP.mouseover.config, config); 

    var mouseovers = [];
    var items = [];
	for (el_type in config.element_types) {
	    items = items.concat(YAHOO.util.Dom.getElementsByClassName(config.css_mouseover_class, config.element_types[el_type], config.root_node));
	}
	for(var i=0, item; item=items[i]; i++) {
	    mouseovers[i] = new JP.mouseover.Mouseover(item, config);
    }
};
//Comment the below line if you don't want the mouseovers to start automatically
YAHOO.util.Event.addListener(window, 'load', JP.mouseover.auto);
